<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<%@ include file="../inc/fileHead.jsp"%>
	</head>
	<body  class="easyui-layout"> 
	<div data-options="region:'center'">
		<table id="dg" title="用户管理" class="easyui-datagrid"
			url="UserAction_findUser" 
			toolbar="#toolbar" pagination="true" rownumbers="true"
			fitColumns="true" singleSelect="true">
			<thead>
				<tr>
					<th field="username" width="50">
						用户名
					</th>
					<th field="password" width="50">
						密码
					</th>
					<th field="sex" width="50">
						性别
					</th>
					<th field="role" width="50">
						角色
					</th>
					<th field="tel" width="50">
						电话
					</th>
				</tr>
			</thead>
		</table>
			<div id="toolbar">
				<a href="javascript:void(0)" class="easyui-linkbutton"
					iconCls="icon-add" plain="true" onclick="newUser()">添加用户</a>
				<a href="javascript:void(0)" class="easyui-linkbutton"
					iconCls="icon-edit" plain="true" onclick="editUser()">修改用户</a>
				<a href="javascript:void(0)" class="easyui-linkbutton"
					iconCls="icon-remove" plain="true" onclick="destroyUser()">删除用户</a>
				<a href="javascript:void(0)" class="easyui-linkbutton"
					iconCls="icon-search" plain="true" onclick="searchForUser()">查询用户</a>
			</div>

			<div id="dlg" class="easyui-dialog"
				style="width: 400px; height: 350px; padding: 10px 20px"
				closed="true" buttons="#dlg-buttons">
				<div class="ftitle">
					用户信息
				</div>
				<form id="fm" method="post" novalidate>
					<div class="fitem">
						<label>
							用户名
						</label>
						<input name="username" class="easyui-validatebox" required="true"
							style="width: 200px;">
					</div>
					<div class="fitem">
						<label>
							密码
						</label>
						<input type="password" name="password" class="easyui-validatebox"
							style="width: 200px;" required="true">
					</div>
					<div class="fitem">
						<label>
							性别
						</label>
						<select name="sex" required="true" class="easyui-combobox"
							style="width: 200px;">
							<option value="男">男</option>
							<option value="女">女</option>
						</select>
					</div>
					<div class="fitem">
						<label>
							角色
						</label>
						<select name="role" required="true" class="easyui-combobox"
							style="width: 200px;">
							<option value="1">PM</option>
							<option value="2">SALES</option>
						</select>
					</div>
					<div class="fitem">
						<label>
							电话
						</label>
						<input name="tel" class="easyui-validatebox" required="true"
							style="width: 200px;">
					</div>
				</form>
			</div>
			<div id="searchdlg" class="easyui-dialog"
				style="width: 400px; height: 280px; padding: 10px 20px"
				closed="true" buttons="#dlg-buttons2">
				<div class="ftitle">
					查询条件
				</div>
				<div class="fitem">
					<label>
						用户名
					</label>
					<input id="q_username" name="q_username" class="easyui-validatebox" />
				</div>
				<div class="fitem">
					<label>
						电话
					</label>
					<input id="q_tel" name="q_tel" class="easyui-validatebox" />
				</div>
			</div>
			<div id="dlg-buttons">
				<a href="javascript:void(0)" class="easyui-linkbutton"
					iconCls="icon-ok" onclick="saveUser()">保存</a>
				<a href="javascript:void(0)" class="easyui-linkbutton"
					iconCls="icon-cancel"
					onclick="javascript:$('#dlg').dialog('close')">取消</a>
			</div>
			<div id="dlg-buttons2">
				<a href="javascript:void(0)" class="easyui-linkbutton"
					iconCls="icon-ok" onclick="searchUser()">查询</a>
				<a href="javascript:void(0)" class="easyui-linkbutton"
					iconCls="icon-cancel"
					onclick="javascript:$('#dlg').dialog('close')">取消</a>
			</div>
			<script type="text/javascript">  
            var url;  
            function newUser(){  
                $('#dlg').dialog('open').dialog('setTitle','添加用户');  
                $('#fm').form('clear');  
                url = 'UserAction_addUser';  
            }  
            function editUser(){  
                var row = $('#dg').datagrid('getSelected');  
                var name = row.username;
                if(name=='admin'){
                	$.messager.alert('提示','超级用户不能修改');  
                	return;
                }
                if (row){  
                    $('#dlg').dialog('open').dialog('setTitle','修改用户');  
                    $('#fm').form('load',row);  
                    url = 'UserAction_updateUser?userid='+row.id;  
                }  
            }  
            function searchForUser(){
            	$('#searchdlg').dialog('open').dialog('setTitle','查询用户');  
                 $('#fm2').form('clear'); 
            }
            function searchUser(){  
                 $('#searchdlg').dialog('close');      // close the dialog
                 $('#dg').datagrid({
                 url:'UserAction_findByParam',  
				 queryParams: {
					q_username: $('#q_username').val(),  
    					 q_tel: $('#q_tel').val() 
				}
				});
            }  
            function saveUser(){  
                $('#fm').form('submit',{  
                    url: url,  
                    onSubmit: function(){  
                    	if($(this).form('validate')){
                    		return true;
                    	}else{
                    		$.messager.alert('提示','请填写完整资料！'); 
                        	return false;
                        }  
                    },  
                    success: function(result){  
                        var result = eval('('+result+')');  
                        if (result.errorMsg){
                        	$.messager.alert('错误',result.errorMsg);    
                        } else {
                        	$.messager.alert('提示','操作成功');  
                            $('#dlg').dialog('close');      // close the dialog  
                            $('#dg').datagrid('reload');    // reload the user data  
                        }  
                    }  
                });  
            }  
            function destroyUser(){  
                var row = $('#dg').datagrid('getSelected');  
                var name = row.username;
                if(name=='admin'){
                	$.messager.alert('提示','超级用户不能删除');  
                	return;
                }
                if (row){  
                    $.messager.confirm('Confirm','确定要删除这个用户?',function(r){  
                        if (r){  
                            $.post('UserAction_deleteUser',{userid:row.id},function(result){  
                                if (result.success){  
                                    $('#dg').datagrid('reload');    // reload the user data  
                                } else {  
                                    $.messager.alert('错误',result.errorMsg); 
                                }  
                            },'json');  
                        }  
                    });  
                }  
            }  
        </script>
			<style type="text/css">
#fm {
	margin: 0;
	padding: 10px 30px;
}

.ftitle {
	font-size: 14px;
	font-weight: bold;
	padding: 5px 0;
	margin-bottom: 10px;
	border-bottom: 1px solid #ccc;
}

.fitem {
	margin-bottom: 5px;
}

.fitem label {
	display: inline-block;
	width: 80px;
}
</style>
</div>
	</body>
</html>